package com.nl.lib_core.view

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import androidx.annotation.ColorRes
import androidx.constraintlayout.widget.ConstraintLayout
import com.alibaba.android.arouter.launcher.ARouter
import com.nl.lib_core.R
import com.nl.lib_core.tool.extends.gone
import com.nl.lib_core.tool.extends.visible

/** 作者: wangfei 创建时间: 2021/9/1 包名: com.nl.lib_core.view 类名: NetErrorView */
class NetErrorView(context: Context, attrs: AttributeSet) : ConstraintLayout(context, attrs) {

  private val mRlNetWorkError: ConstraintLayout
  private val checkToLocal: Button

  init {
    inflate(context, R.layout.core_view_net_error, this)
    mRlNetWorkError = findViewById(R.id.rl_net_error_root)
    checkToLocal = findViewById(R.id.checkToLocal)
  }

  /** 设置点击刷新按钮键 */
  fun setRefreshBtnClickListener(listener: OnClickListener) {
    findViewById<View>(R.id.btn_net_refresh).setOnClickListener { v -> listener.onClick(v) }
    checkToLocal.setOnClickListener {
      ARouter.getInstance().build("/main_local/mainRoot_local").navigation()
      (context as Activity).finish()
    }
  }
  /** 隐藏切换单机按钮 */
  fun hideCheckToLocalBTN() {
    checkToLocal.gone()
  }

  /** 展示切换单机按钮 */
  fun showCheckToLocalBTN() {
    checkToLocal.visible()
  }

  /** 设置错误背景颜色 */
  fun setNetErrorBackground(@ColorRes colorResId: Int) {
    mRlNetWorkError.setBackgroundColor(resources.getColor(colorResId))
  }
}
